package org.sunflower.t.wxpaytest.infrastructure;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;
import org.sunflower.t.wxpaytest.infrastructure.annotation.EnableWxPayV3;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @description:
 * @author: TanJingyu
 * @create: 2021-09-26 14:57
 **/
public class WxPayV3ImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return StringUtils.toStringArray(getConfigurations(importingClassMetadata));
    }

    private Collection<String> getConfigurations(AnnotationMetadata importingClassMetadata) {
        AnnotationAttributes attributes = importingClassMetadata.getAnnotations().get(EnableWxPayV3.class).asAnnotationAttributes();

        Collection<String> result = new ArrayList<>();

        attributes.forEach((key, value) -> {
            switch (key) {
                case "autoEncrypt":
                    if ((boolean) value) {
                        result.add("1");
                    }
                    break;
                case "autoDecrypt":
                    if ((boolean) value) {
                        result.add("org.sunflower.t.wxpaytest.infrastructure.aop.AutoDecryptAspect");
                    }
                    break;
                default:
            }
        });

        return result;
    }
}
